package com.weetop.cfw.widget;

import android.view.View;
import android.widget.LinearLayout;

import com.luseen.autolinklibrary.AutoLinkMode;
import com.luseen.autolinklibrary.AutoLinkTextView;
import com.weetop.cfw.R;

/**
 * 启动页隐私弹框
 *
 * @author yuwuxin
 */
public class SplashAgreementDialog extends BaseDialog implements View.OnClickListener {

    LinearLayout ll_dialog;

    AutoLinkTextView auto_link_tv_splash_agreement_content_1;

    LinearLayout ll_dialog_again;

    private onClickButtonListener onClickButtonListener;

    public static SplashAgreementDialog newInstance() {
        return new SplashAgreementDialog();
    }

    public void setOnClickButtonListener(SplashAgreementDialog.onClickButtonListener onClickButtonListener) {
        this.onClickButtonListener = onClickButtonListener;
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_splash_agreement;
    }

    @Override
    public void initView() {
        setDialogClickAndBackInvalid();


        ll_dialog = rootView.findViewById(R.id.ll_dialog);
        auto_link_tv_splash_agreement_content_1 = rootView.findViewById(R.id.auto_link_tv_splash_agreement_content_1);
        ll_dialog_again = rootView.findViewById(R.id.ll_dialog_again);
        rootView.findViewById(R.id.tv_dialog_disagree).setOnClickListener(this);
        rootView.findViewById(R.id.tv_dialog_agree_continue).setOnClickListener(this);
        rootView.findViewById(R.id.tv_dialog_again_exit_app).setOnClickListener(this);
        rootView.findViewById(R.id.tv_dialog_again_do_agree).setOnClickListener(this);
        InitSplashAgreementContent();
    }

    private void InitSplashAgreementContent() {
        auto_link_tv_splash_agreement_content_1.addAutoLinkMode(AutoLinkMode.MODE_CUSTOM);
        auto_link_tv_splash_agreement_content_1.setCustomRegex("《(.*?)》");
        auto_link_tv_splash_agreement_content_1.setCustomModeColor(getResources().getColor(R.color.text_red_CA3232));
        auto_link_tv_splash_agreement_content_1.setAutoLinkText(getString(R.string.text_splash_agreement_content_1));
        auto_link_tv_splash_agreement_content_1.setAutoLinkOnClickListener((autoLinkMode, matchedText) -> {
            if (autoLinkMode == AutoLinkMode.MODE_CUSTOM) {
                if ("《铭豪厂房网用户协议》".contains(matchedText)) {
                    onClickButtonListener.onClickLicence();
                } else if ("《铭豪厂房网隐私政策》".contains(matchedText)) {
                    onClickButtonListener.onClickPrivacy();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_dialog_disagree:   //不同意
                ll_dialog.setVisibility(View.GONE);
                ll_dialog_again.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_dialog_agree_continue:     //同意并继续
                dismiss();
                onClickButtonListener.onClickAgreeContinue();
                break;
            case R.id.tv_dialog_again_exit_app:     //退出应用
                dismiss();
                onClickButtonListener.onClickExitApp();
                break;
            case R.id.tv_dialog_again_do_agree:     //去同意
                ll_dialog_again.setVisibility(View.GONE);
                ll_dialog.setVisibility(View.VISIBLE);
                break;
        }
    }

    public interface onClickButtonListener {

        /**
         * 点击同意并继续
         */
        void onClickAgreeContinue();

        /**
         * 点击退出应用
         */
        void onClickExitApp();

        /**
         * 点击隐私政策
         */
        void onClickPrivacy();

        /**
         * 点击用户协议
         */
        void onClickLicence();
    }
}
